//
//  ImageBrowserView.m
//  Customized
//
//  Created by AceWei on 2017/8/11.
//  Copyright © 2017年 陈凯旋. All rights reserved.
//

#import "ImageBrowserView.h"
#import <<#header#>>

@interface ImageBrowserView ()<UIScrollViewDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, copy) NSArray <NSString *> *urls;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, assign) CGSize containerSize;

@end

@implementation ImageBrowserView

//+ (instancetype)showImages:(NSArray <NSString *> *)urls
//                      size:(CGSize)size
//                     index:(NSInteger)index
//{
//    ImageBrowserView *vi = [[ImageBrowserView alloc] init];
//    vi.urls = urls;
//    vi.page = index;
//    vi.containerSize = size;
//    
//    [vi setupUI];
//    [vi addImagesView:urls];
//    
//    return vi;
//}

+ (instancetype)createWithSize:(CGSize)size
{
    ImageBrowserView *vi = [[ImageBrowserView alloc] init];
    vi.containerSize = size;
    
    [vi setupUI];
    
    return vi;
}


- (void)addImageUrls:(NSArray <NSString *> *)urls
{
    [self addImageUrls:urls index:1];
}


- (void)addImageUrls:(NSArray<NSString *> *)urls
               index:(NSInteger)index
{
    self.urls = urls;
    self.page = index;
    
    for (UIView *vi in [_scrollView subviews]) {
        if ([vi isKindOfClass:[UIImageView class]]) {
            [vi removeFromSuperview];
        }
    }
    
    [self addImagesView:_urls];
}


- (void)setupUI
{
    [self addScrollView];
    [self addPageControl];
}


- (void)addScrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
    }

    if (![_scrollView isDescendantOfView:self]) {
        [self addSubview:_scrollView];
    }
    
    
    _scrollView.frame = CGRectMake(0, 0, _containerSize.width, _containerSize.height);
    
    _scrollView.delegate = self;
    
    _scrollView.contentSize = CGSizeZero;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.layer.masksToBounds = YES;
    
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
}


- (void)addImagesView:(NSArray <NSString *>*)imgUrlArray
{
    _scrollView.contentSize = CGSizeMake(_containerSize.width*(imgUrlArray.count+2), 0);
    
    for (NSInteger i=0; i<imgUrlArray.count; i++) {
        NSString *str = imgUrlArray[i];
        [self addSingleImageWithURL:str
                              index:i+1];
    }
    
    NSUInteger count = imgUrlArray.count;
    if (count > 0) {
        {
            NSString *str = imgUrlArray[count-1];
            [self addSingleImageWithURL:str
                                  index:0];
        }
        
        {
            NSString *str = imgUrlArray[0];
            [self addSingleImageWithURL:str
                                  index:count+1];
        }
    }
    
    
    [self goToPageNum:_page];
    
    [self setupPageControl];
}


- (void)addSingleImageWithURL:(NSString *)url
                        index:(NSUInteger)index
{
    UIImageView *imgV = [[UIImageView alloc] init];
    imgV.tag = index;
    
    NSURL *imgUrl = [NSURL URLWithString:url];
    [imgV sd_setImageWithURL:imgUrl];
    
    [self.scrollView addSubview:imgV];
    
    imgV.frame = CGRectMake(index * _containerSize.width, 0, _containerSize.width, _containerSize.height);
}


- (void)addPageControl
{
    if (!self.pageControl) {
        self.pageControl = [[UIPageControl alloc]init];
    }
    
    if (![self.pageControl isDescendantOfView:self]) {
        [self addSubview:self.pageControl];
    }
    
    
    self.pageControl.currentPageIndicatorTintColor = RGBA(0, 186, 186, 1);
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.pageControl.hidden = YES;
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
        make.centerX.equalTo(self);
        make.bottom.offset(0);
    }];
}


- (void)setupPageControl
{
    self.pageControl.hidden = NO;
    self.pageControl.numberOfPages = _urls.count;
    self.pageControl.currentPage = _page-1;
}



#pragma mark - 事件
- (void)goToPageNum:(NSUInteger)index
{
    CGPoint position = CGPointMake(_containerSize.width*index+1, 0);
    [_scrollView setContentOffset:position animated:NO];
}


#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    _page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = _page-1;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_page == _urls.count+1) {
        CGPoint position = CGPointMake(1*_containerSize.width, 0);
        [_scrollView setContentOffset:position animated:NO];
    }
    
    if (_page == 0) {
        CGPoint position = CGPointMake(_urls.count*_containerSize.width, 0);
        [_scrollView setContentOffset:position animated:NO];
    }
}


@end
